#include "head.h"

int main(int argc, char *argv[]) {
    AddressBook *head = NULL;
    Login *login_list = NULL;
    int count = 0;
    int lg_count = 0;
    int flag = 0;

    count = read_contacts_from_file("contacts.txt", &head);
    lg_count = read_logins_from_file("login.txt", &login_list);

    while (1) {
        int choice;
        printf("1. 注册\n");
        printf("2. 登录\n");
        printf("3. 退出\n");
        printf("请输入你的选择：");
        if (scanf("%d", &choice) != 1) {
            printf("输入错误，请重新输入\n");
            while (getchar() != '\n');
            continue;
        }
        switch (choice) {
            case 1:
                register_user(&login_list, &lg_count);
                break;
            case 2:
                flag = login_user(login_list);
                if (flag == 1) {
                    while (1) {
                        printf("1. 添加联系人\n");
                        printf("2. 查看联系人\n");
                        printf("3. 修改联系人\n");
                        printf("4. 删除联系人\n");
                        printf("5. 插入联系人\n");
                        printf("6. 展示所有联系人\n");
                        printf("7. 导出\n");
                        printf("8. 按姓名排序\n");
                        printf("9. 返回主菜单\n");
                        printf("请输入你的选择：");
                        if (scanf("%d", &choice) != 1) {
                            printf("输入错误，请重新输入\n");
                            while (getchar() != '\n');
                            continue;
                        }
                        switch (choice) {
                            case 1:
                                add_contact(&head, &count);
                                break;
                            case 2:
                                search_contact(head);
                                break;
                            case 3:
                                modify_contact(head);
                                break;
                            case 4:
                                delete_contact(&head, &count);
                                break;
                            case 5:
                                insert_contact(&head, &count);
                                break;
                            case 6:
                                show_all_contacts(head);
                                break;
                            case 7:
                                export_contacts(head);
                                break;
                            case 8:
                                sort_contacts(head);
                                break;
                            case 9:
                                flag = 0;
                                break;
                            default:
                                printf("输入错误，请重新输入\n");
                        }
                        if (flag == 0) {
                            break;
                        }
                    }
                }
                break;
            case 3:
                printf("退出成功\n");
                free_all_contacts(&head);
                free_all_logins(&login_list);
                return 0;
            default:
                printf("输入错误，请重新输入\n");
        }
    }
    return 0;
}